/**
 * Created by jinxs on 14-11-12.
 */
(function ($) {
    /**
     * 缓存名称
     * @type {string}
     * @private
     */
    var _cacheName = 'uiUEditor';
    /**
     * @class 富文本编辑器组件
     * @constructor uiUEditor
     * @summary 构造函数
     * @param [options] {Object|String} 组件配置参数|要调用的组件方法名
     * @param [param] {Object} 表示要调用的组件方法的参数
     * @example
     *  //采用默认值
     * $('#tab').uiUEditor();
     * //设置组件参数
     *  $('#tab').uiUEditor({autoFit:'true'});
     * //调用组件方法
     * $('#tab').uiUEditor('getEditor','');
     */
    $.fn.uiUEditor = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.uiUEditor.methods[options](this, param);
        }
        return this.each(function(){
            var me = $(this);
            var state = me.data(_cacheName);

            options = options || {};
            if (state) {
                $.extend(state.options, options);
            } else {
                me.data(_cacheName, {
                    options: $.extend({}, $.fn.uiUEditor.defaults, me.parseUIConfig(), options)
                });
            }
            _init(me);
            _resize(me);
        });
    };
    /**
     * 方法
     * @lends uiUEditor.prototype
     */
    $.fn.uiUEditor.methods = {
        /**
         * @summary 获取组件配置参数
         * @function
         * @name uiUEditor#options
         * @returns {Object}
         * @example
         *  $('#btn').uiUEditor('options');
         */
        options: function (target) {
            return $.data(target[0], _cacheName).options;
        },
        /**
         * @summary 获取编辑器
         * @function
         * @name uiUEditor#getEditor
         * @returns {Object}
         */
        getEditor: function (target, config) {
            var targetID = target.getElemID();
            config = config || {};
            return UE.getEditor($.getIDByParentID(targetID, $.uiTemplate.uiUEditor.editorID), config);
        }
        , enable: function (target) {
            var editor=target.uiUEditor('getEditor');
            editor.ready(function () {
                editor.setEnabled();
            });
            return target;
        }
        , disable: function (target, except) {
            var editor=target.uiUEditor('getEditor');
            editor.ready(function () {
                editor.setDisabled(except);
            });
            return target;
        }
        , setReadonly: function (target, readonly) {
            var targetID = target.getElemID();
            var editor=target.uiUEditor('getEditor');
            editor.ready(function () {
                var content = editor.getContent();
                if(readonly) {
                    var reg = new RegExp('({uiUEditor})', 'g');
                    var cont=$($.uiTemplate.uiUEditor.contentHtml.replace(reg, targetID));
                    cont.append(content);
                    target.append(cont);
                    editor.setHide();
                    var starId=target.attr('id')+'-star';
                    $('#'+starId).remove();
                }else{
                    $.getByParentID(targetID, $.uiTemplate.uiUEditor.contentID).remove();
                    editor.setShow();
                }
            });
            return target;
        }
        , destroy: function (target) {
            var editor=target.uiUEditor('getEditor');
            editor.ready(function () {
                target.uiUEditor('getEditor').destroy();
                target.empty();
            });
            return target;
        }
    };
    /**
     * 默认配置
     * @lends uiUEditor.prototype
     */
    $.fn.uiUEditor.defaults =
    {
        /**
         * @type Boolean
         * @default true
         * @summary 是否自适应宽高
         */
        autoFit: true,
        config: {}
    };
    //////--------------------------------内部方法------------------------------
    /**
     * 创建
     * @param target 目标对象
     */
    var _init = function (target) {
        var state = $.data(target[0], _cacheName);
        var options = state.options;
        target.setElemID();
        target.addClass('ui_ueditor');
        var content=target.html();
        target.empty();
        var targetID = target.getElemID();
        var reg = new RegExp('({uiUEditor})', 'g');
        target.append($.uiTemplate.uiUEditor.editorHtml.replace(reg, targetID));
        options.config.initialContent=content;
        target.uiUEditor('getEditor', options.config);
        target.bind('_resize', function () {
            _resize(target);
        });
        return target;
    };
    var _resize = function (target) {
        var state = $.data(target[0], _cacheName);
        var options = state.options;
        if (options.autoFit) {
            target.autoFit(true, false);
            target.uiUEditor('getEditor').ready(function () {
                var targetID = target.getElemID();
                var list = $.getByParentID(targetID, $.uiTemplate.uiUEditor.editorID);
                var toolbarbox = list.find(".edui-editor-toolbarbox");
                var iframeholder = list.find(".edui-editor-iframeholder");
                var bottomContainer = list.find(".edui-editor-bottomContainer");
                list.add(list.children()).width(target.width());
                iframeholder.height(target.height() - toolbarbox.height() - bottomContainer.height());
            });
        }
    };
})(jQuery);
